package com.chmingx.springcloud.contoller;

import com.chmingx.springcloud.service.PaymentService;
import entities.CommonResult;
import entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/payment/create")
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        System.out.println(payment);
        int result = paymentService.create(payment);
        if (result > 0) {
            return new CommonResult<>(200, "insert data success, server port: " + serverPort, result);
        } else {
            return new CommonResult<>(400, "fail to insert", null);
        }
    }

    @GetMapping("/payment/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        System.out.println(id);
        Payment payment = paymentService.getPaymentById(id);
        System.out.println(payment);
        if (payment != null) {
            System.out.println(payment);
            return new CommonResult<>(200, "succeed querying, server port: " + serverPort, payment);
        } else {
            return new CommonResult<>(444, "no such data", null);
        }

    }
    
    @GetMapping("/payment/lb")  // 用于自定义轮询负载均衡，获取lb节点
    public String getPaymentLB() {
        return serverPort;
    }
}
